
// ------------- GLOBAL VARS ----------------------
var map, startCenter;


(function ($) {
  
  /**
   * Initializes Map
   */
  function mapInit( startCenter ) {
    // Setting container for map
    var mapCanvas = $("#map_canvas")[0];
    
    // Creating an option object for the map  
    var mapOptions = {
      backgroundColor: '#000000',
      center: startCenter,
      zoom: 2,
      mapTypeControlOptions: {    
        // Removing all usual map types from list
        mapTypeIds: "demo"
      },
      mapTypeControl: false,      // Hiding the map type control
      streetViewControl: false,   // Hiding the StreetView control
      
      // Positioning the controls 
      panControlOptions: {
        position: google.maps.ControlPosition.LEFT_CENTER
      },
      zoomControlOptions: {
        position: google.maps.ControlPosition.LEFT_CENTER
      }     
    };
    
    // Initializing the map  
    map = new google.maps.Map(mapCanvas, mapOptions);
  }
  
  // --------------------- START IMAGEMAPTYPE ---------------------------------
  
  /**
   *  Initializes the ImageMapType
   */
  function mapTypeInit() {
    // Creating an option object for the ImageMapType
    var demoTypeOptions = {
      getTileUrl: mapTypeGetTileUrl,
      tileSize: new google.maps.Size(256, 256),
      isPng: true,
      maxZoom: 5,     // range of
      minZoom: 1,     // zoom levels
      name: "Demo"    // label for mapTypeControl if displayed
    };
    
    // Creating the ImageMapType object
    var demoMapType = new google.maps.ImageMapType(demoTypeOptions);
    // Adding "demo" to the mapTypes register
    map.mapTypes.set('demo', demoMapType);
    // Activating the "demo" MapType
    map.setMapTypeId('demo');
  }
  
  
  /**
   *  Builds url for background
   */
  function mapTypeGetTileUrl( coord, zoom ) {
    var backgroundPath = "img/bg";    // path to the background folder
    
    // Finding out if map is out of boundaries and if backgrounds should be repeated.
    // If not no background url is returned.
    var normalizedCoord = mapTypeGetNormalizedCoord(coord, zoom);
    if (!normalizedCoord) {
      return false;
    }
    
    // Calculating coordinates and return background url
    var bound = Math.pow(2, zoom);
    var xCoord = normalizedCoord.x;
    var yCoord = (bound - normalizedCoord.y - 1);
    return backgroundPath + "/" + zoom + "/" + xCoord + "/" + yCoord + ".png";
  }
  
  
  /**
   *  Normalizes the coords that tiles repeat across the x axis (horizontally)
   *  like the standard Google map tiles.
   */
  function mapTypeGetNormalizedCoord( coord, zoom ) {
    var y = coord.y;
    var x = coord.x;
  
    // tile range in one direction range is dependent on zoom level
    // 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc
    var tileRange = 1 << zoom;
    
    // don't repeat across y-axis
    if (y < 0 || y >= tileRange)
      return null;                                  
    
    // repeat across x-axis
    if (x < 0 || x >= tileRange)
      x = (x % tileRange + tileRange) % tileRange;  
  
    return { x: x, y: y };
  }
  
  // --------------------- IMAGEMAPTYPE END -----------------------------------
  
  $(document).ready(function() { 
    // coordinates for center of the map (more or less)
    startCenter = new google.maps.LatLng(-60.86391376572419, -18.652343749999996);  
    mapInit(startCenter);
    mapTypeInit();
  });  
  
})(jQuery);